﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;

namespace Helper.Infrastructure.Reporting
{
    public class Streaming
    {
        public string MimeType { get; set; }

        public Stream StreamReport { get; set; }

        public Streaming()
        {
        }

        public Streaming(Stream stream, string mime)
        {
            this.MimeType = mime;
            this.StreamReport = stream;
        }

        public static Streaming GetFileFromReport(ReportClass rptReport, ReportType type)
        {
            Stream strResult = Stream.Null;

            string fileMime = string.Empty;

            switch (type)
	        {
		        case ReportType.AsExcel:
                    strResult = rptReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
                    fileMime = "application/excel";
                 break;
                case ReportType.AsPDF:
                    strResult = rptReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                    fileMime = "application/pdf";
                 break;
                case ReportType.AsWord:
                    strResult = rptReport.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
                    fileMime = "application/msword";
                 break;
                default:
                 break;
	        }

            if(strResult != Stream.Null && fileMime != string.Empty)
                return new Streaming(strResult, fileMime);
            return null;
        }
    }
}
